1 using UnityEngine;
2 using System.Collections;
3 using UnityStandardAssets.CrossPlatformInput;
4 using UnityEngine.SceneManagement;
5 using UnityEngine.UI;
6
7 public class IronManBehaviorScript : MonoBehaviour {
8
9 public float speed = 2.0f;
10 public GameObject gameOverPanel;
11 PlayerHealth playerHealth;
12 public int score;
13 public Text scoreText;
14
15 Vector3 movement;
16 Rigidbody playerRigidBody;
17 bool isMoving = false;
18 Animator anim;
19 int floorMask;
20 float camRayLength = 100.0f;
21 public bool isEnabled = true;
22 public bool gameOver = false;
23
24 public AudioClip gameOverClip;
25 public AudioSource audioSource;
26
27 // Use this for initialization
28 void Start () {
29 gameOverPanel.SetActive (false);
30 GameObject player = GameObject.FindGameObjectWithTag ("Player");
31 playerHealth = player.GetComponent<PlayerHealth> ();
32 score = 0;
33 scoreText.text = "Score: 0";
34 audioSource = GetComponent<AudioSource> ();
35
36 AdsSetup ();
37
38 }
39
40 // Update is called once per frame
41 void Update () {
42 if (playerHealth.currentHealth <= 0) {
43 isEnabled = false;
44 if (!gameOver) {
45 Invoke ("DisplayGameOver", 1.0f);
46 }
47 }
48
49 scoreText.text = "Score: " + score.ToString ();
50
51 }
52
53 void Awake() {
54 playerRigidBody = GetComponent<Rigidbody> ();
55 anim = GetComponent<Animator> ();
56 floorMask = LayerMask.GetMask ("Floor");
57 }
58
59 void FixedUpdate (){
60 if (isEnabled == false)
61 return;
62 // we need to get a hold of which keys (input) has been used by the Player
63 float h = CrossPlatformInputManager.GetAxisRaw("Horizontal");
64 float v = CrossPlatformInputManager.GetAxisRaw ("Vertical");
65
66 Move (h, v);
67
68 if (h != 0 || v != 0) {
69 isMoving = true;
70 } else {
71 isMoving = false;
72 }
73 Animating ();
74 Turning ();
75 }
76
77
78 void Move( float h, float v){
79 movement.Set (-v, 0f, h);
80
81 movement = movement.normalized * Time.fixedDeltaTime * speed;
82
83 playerRigidBody.MovePosition (transform.position + movement);
84
85
86 }
87
88
89 void Animating(){
90 // if the character is moving, then play the walking animation
91 // if not, go to idle animation
92 if (isMoving == true) {
93 // we play the walking anim
94 anim.SetFloat ("speed", 1);
95 } else {
96 anim.SetFloat ("speed", 0);
97 }
98 }
99
100 public void Turning(){
101 // 1) get to know where the mouse is located at
102 // if it is in range, then rotate the character towards the mouse
103
104 #if !MOBILE_INPUT
105 Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
106
107 RaycastHit floorHit;
108 if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
109 Vector3 playerToMouse = floorHit.point - transform.position;
110 playerToMouse.y = 0.0f;
111 Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
112 playerRigidBody.MoveRotation(newRotation);
113
114 }
115 #else
116 Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Horizontal"), 0f, CrossPlatformInputManager.GetAxisRaw("Vertical"));
117 if(turnDir != Vector3.zero){
118 Vector3 playerToMouse = (transform.position + turnDir) - transform.position;
119 playerToMouse.y = 0;
120 Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
121 playerRigidBody.MoveRotation(newRotation);
122
123 }
124 #endif
125 }
126
127
128 public void DisableMovement(){
129 isEnabled = false;
130 }
131
132 public void RestartGame(){
133 SceneManager.LoadScene ("scene-ironman");
134 }
135
136 void DisplayGameOver(){
137 gameOver = true;
138 gameOverPanel.SetActive (true);
139 audioSource.PlayOneShot (gameOverClip);
140 // todo: display ads
141 //Advertisement.Show();
142 }
143
144 void AdsSetup(){
145 //Advertisement.Initialize ("1041425", true);
146 }
147
148 }